Learn Cocoa on the Mac by Jack Nutting & Peter Clark

Learn Cocoa on the Mac by Jack Nutting & Peter Clark

Author:Jack Nutting & Peter Clark
Language: eng
Format: epub, mobi
ISBN: 9781430245421
Publisher: Apress


Figure 9-8 . Setting bindings for the Mythical Bands table view

Lastly, we’ll deal with the Search box. Select the Search field at the upper right corner of the screen, and open the Bindings Inspector. Near the bottom of the Bindings Inspector is a section titled Predicate. Open it up and check the Bind to checkbox, binding it to Mythical Bands. The Controller Key should say “filterPredicate.” Set the Display Name to “Name” and the predicate format to “name contains[c] $value” where $value indicates the contents of the search box, and name indicates the MythicalBand entity attribute that should be searched. This is just like the binding we did for the Search field in Chapter 8.

For the buttons, we’ll use the same process as in the last chapter, using a combination of target-action and Cocoa bindings. We’ll use target-action for when a button is pressed, and Cocoa bindings to enable or disable the buttons as appropriate. We’ll start with the actions. Select the Add button, and control-drag to the Mythical Bands array controller. Select the add: action from the pop-up menu. Then, select the Remove button, and Control-drag to the same place. This time, select the remove: action. To enable and disable the buttons based on the selection in the table view, we need to use the Bindings Inspector. Select the Add button, open the Bindings Inspector, and expand the Enabled section under Availability. Check the Bind to checkbox, set the pull-down menu to Mythical Bands, and change the Controller Key field to read “canAdd.” For the Remove button, do the same thing, but use “canRemove” for the Controller Key field.

Now save your changes, and run the app. The new window appears, and we can add some bands, edit their names directly in the table view, and save our changes.

GIVING USEFUL NAMES TO ARRAY CONTROLLERS

There’s a change we want to recommend that you make in your nib file that will be helpful when configuring the new bindings we’re about to create. It won’t affect the behavior of the app, but you’ll probably be glad that you made the change, and the text going forward assumes that you’ve made this update.

The connection between the Core Data infrastructure and the UI all runs through an NSArrayController. The NSArrayController that we set up in the last chapter is named Mythical Person Array Controller. This is a long name, and is a bit longer than what the Binding Inspector in Xcode is really designed to show. In fact, it barely fits into the pop-up buttons in the Bindings Inspector, and they make all of the summary info shown for each bound attribute overflow, with some of its text being replaced with ellipses. For the Mythical Bands array controller, we didn’t put the “Array Controller” in the name, which makes it much easier to work with.

You can improve this situation by changing the name of the controller. Changing the names won’t modify any of the bindings you’ve configured or change anything for your application’s users. The only one who’ll see the change at all is you, but remember that you’re important, too.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.